package com.hfzy.ihk.common.util.psw;

import com.hfzy.ihk.common.util.Utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * Created by Administrator on 2016/5/6.
 */
public class Encrypter {

    /*拼接需要加密的内容*/
    public static String genKey(String... params){

        String key = "cloudCall:";
        for(String s:params) {
            key += s;
        }
        key = key + Utils.date2String(new Date(),Utils.DATE_FORMAT_DATEONLY);

        return key;
    }

    // 用MD5算法加密字符串
    public static String md5Encrypt(String s) {
        //获取支持MD5算法的MessageDigest//提供信息摘要算法
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            // 获取BASE64Encoder对象，用Base64格式对字符串进行编码
            BASE64Encoder base64Encoder = new BASE64Encoder();
            // //  digest()执行诸如填充之类的最终操作完成哈希计算.然后对字符串进行加密，并将加密后的字符中按Base64格式进行编码
            return base64Encoder.encode(md5.digest(s.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    // 用MD5算法加密字符串
    public static String md5(String s) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(s.getBytes("utf-8"));
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密出现错误");
        }
    }

    /**base64加密*/
    public static String encode(String url){
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(url.getBytes());
    }
    /**base64解密*/
    public static String decode(String data) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(data).toString();
    }

    /*福建电信密码加密*/
    public static String sha1Encrypt(String Passwd) throws NoSuchAlgorithmException {
        MessageDigest alg = MessageDigest.getInstance("SHA-1");
        alg.update(Passwd.getBytes());
        byte[] bts = alg.digest();
        String result = "";
        String tmp = "";
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) result += "0";
            result += tmp;
        }
        return result;
    }

      /*  public static void main(String[] args){
        String ps = "admin";
        System.out.println(Encrypter.md5Encrypt(ps));//ISMvKXpXpadDiUoOSoAfww==

        String ps1 = "winsnlife";
        System.out.println(Encrypter.md5Encrypt(ps1));//a6TxmHiSqrfOeUVCuPz3TQ==
    }*/
}
